<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/crypto-js.js"></script>
  <script src="../Scripts/secret.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <script src="../Scripts/datav.min.vue.js"></script>
  <script src="../Scripts/lodash.min.js"></script>
  <title>APS排产统计看板</title>
  <style>
    html {
      background-image: url(../images/bg.jpg);
      color: #fff;
    }

    .el-form-item__label {
      color: #fff;
    }

    .header-title {
      font-size: 24px !important;
    }

    .el-form-item {
      margin-bottom: 0 !important;
    }

    .chart-title {
      text-align: center;
      color: #fff;
      padding-top: 5px;
      font-size: 18px;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak>
    <div style="padding:0 5px;">
      <div class="parallelogram header-outer">
        <el-row>
          <el-col :span="7" style="padding-left: 35px;">
            <img src="../images/aeec_logo.png" height="60" alt="黎阳LOGO" />
          </el-col>
          <el-col :span="10" class="header-title">
            <div class="parallelogram header-inner">
              {{queryInfo.factoryname}}APS排产统计看板
            </div>
          </el-col>
          <el-col :span="7" class="currentTime" style="padding-right: 35px;font-size: 18px;">
            {{currentTime}}
          </el-col>
        </el-row>
      </div>

      <!-- 查询条件区start -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'" :rules="rules"
        style="margin:10px 10px 0 10px;">
        <el-form-item label="加工结束时间:" prop="endtime">
          <el-date-picker v-model="queryInfo.endtime" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>

        <el-form-item prop='containername' label="主卡批次号:">
          <el-autocomplete v-model.trim="queryInfo.pcontainername" placeholder="请输入" :trigger-on-focus="false"
            :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500" style="width:300px;">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop='containername' label="热表批次号:">
          <el-autocomplete v-model.trim="queryInfo.subcontainername" placeholder="请输入" :trigger-on-focus="false"
            :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500" style="width:400px;">
          </el-autocomplete>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="searchData">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 查询条件区end -->

      <!-- 第一行图表start -->
      <el-row :gutter="10" style="margin-top:10px;">
        <el-col :span="8">
          <dv-border-box-10 ref="factoryChart">
            <div class="chart-title">分厂排产统计</div>
            <div :style="{height:chartHeight + 'px'}" id="factoryChart"></div>
          </dv-border-box-10>
        </el-col>
        <el-col :span="16">
          <dv-border-box-10 ref="workcenterChart">
            <div class="chart-title">各工区排产统计</div>
            <div :style="{height:chartHeight + 'px'}" id="workcenterChart"></div>
          </dv-border-box-10>
        </el-col>
      </el-row>
      <!-- 第一行图表end -->

      <!-- 第二行图表start -->
      <div style="margin-top:10px;">
        <dv-border-box-10 ref="teamChart">
          <div class="chart-title">各班组排产统计</div>
          <div :style="{height:chartHeight + 'px'}" id="teamChart"></div>
        </dv-border-box-10>
      </div>
      <!-- 第二行图表end -->

      <!-- 数据钻取 -->
      <el-dialog :title="chartDetail.title" :width="'98%'" :top="'10px'" :center="'center'"
        :visible.sync="chartDetail.dialogVisiable">
        <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
          element-loading-background="rgba(0,0,0,0.8)" :data="chartDetail.dataList" stripe border>
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-download" size="mini" @click="exportDetail">
                导出
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="主制分厂" prop="fromfactoryname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="承制分厂" prop="tofactoryname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="工区" prop="workcentername" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="班组" prop="teamname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="订单号" prop="mfgordername" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="主卡流水卡号" prop="pcontainername" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="主卡工序" prop="pworkflowstepname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="热表批次号" prop="rbcontainername" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="热表工序" prop="sworkflowstepname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="数量" prop="qty" width="60" show-overflow-tooltip></el-table-column>
          <el-table-column label="零件图号" prop="productname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="零件名称" prop="productdescription" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="计划发出时间" prop="pstarttime" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="计划完成时间" prop="pendtime" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="加工开始时间" prop="starttime" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="加工结束时间" prop="endtime" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="转工发出时间" prop="senddate" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="转工接收时间" prop="toacceptdate" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="转工完成时间" prop="completedate" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="转工转入时间" prop="recivedate" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="完工时间" prop="checkdate" width="160" :formatter='replaceDate'></el-table-column>
          <el-table-column label="状态" prop="status" width="80"></el-table-column>
          <el-table-column label="超期天数" prop="delaydays" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column label="排产报工人" prop="toperson" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column label="实际报工人" prop="reportemployeename" width="80" show-overflow-tooltip></el-table-column>
        </el-table>
        <el-pagination @current-change="handleChartDetailCurrentChange"
          :current-page="chartDetail.queryInfo.currentPage" :page-sizes="[chartDetail.queryInfo.pageSize]"
          :page-size="chartDetail.queryInfo.pageSize" layout="prev,pager,next,jumper,sizes,total"
          :total="chartDetail.total">
        </el-pagination>
      </el-dialog>
    </div>
  </div>
  <script src="../Scripts/echarts/echarts-5.4.0.min.js"></script>
  <script type="module">
    import { barOption, colorList } from '../Scripts/hooks/chartConfig.js'
    import { exportExcelData, getParams, fetchSuggestion } from '../Scripts/hooks/useCommonMethod.js'
    import { getFactory } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import { splitDate, replaceDate } from '../Scripts/hooks/format.js'
    const app = new Vue({
      el: '#app',
      data: {
        splitDate, replaceDate,
        currentTime: '',
        loading: false,
        loadingText: '数据加载中...',
        queryInfo: {
          factoryid: '',
          factoryname: '',
          endtime: '',
          pcontainername: '',
          subcontainername: ''
        },
        chartLoadingConfig: { text: '数据查询中...', effect: 'whirling', maskColor: 'transparent', textColor: '#fff' },
        chartHeight: 0,
        chartRow: 2,
        rules: {
          endtime: [{ required: true, message: '加工结束时间不能为空', trigger: 'change' }]
        },
        chartDetail: {
          dialogVisiable: false,
          queryInfo: { currentPage: 1, pageSize: 12, seriesName: '', name: '', orgType: '' },
          dataList: [], title: '', total: 0
        },
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] }
      },
      methods: {
        // 查询批次号
        searchContainerAsync(value, callback) {
          fetchSuggestion({ actionName: 'getContainernameAsync', name: 'containername', value, callback });
        },
        searchData() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (!valid) { return this.$message.error('必填项不能为空'); }
            this.getFactoryData();
            this.getWorkcenterData();
            this.getTeamData();
          })
        },
        // 分厂排产统计
        async getFactoryData() {
          const dom = document.getElementById("factoryChart");
          echarts.dispose(dom);
          let chart = echarts.init(dom);
          chart.showLoading(this.chartLoadingConfig);

          const { data: { data, meta, total } } = await axios.post('../handler/board/RbApsShipFinishDashBoard.ashx?action=getFactoryData', {
            type: 'getFactoryData',
            ...this.queryInfo
          })
          chart.hideLoading();
          if (meta.status == 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          echarts.dispose(dom);
          chart = echarts.init(dom);

          let arr_completerate = [], arr_intimerate = [], arr_delaydayssum = [], arr_notfinishrate = [];
          for (const i of data) {
            arr_completerate.push(i.finishrate);//完成率
            arr_notfinishrate.push(i.notfinishrate);//未完成率
            arr_intimerate.push(i.intimefinishrate);//按时完成率
            arr_delaydayssum.push(i.delaydays);//超期天数
          }

          let option = barOption();
          option.grid = { left: '3%', right: '3%', bottom: '5%', containLabel: true };
          option.yAxis[0].axisLabel.formatter = function (data) { return data + "%"; };//y轴文字追加%
          option.yAxis[1].axisLabel.formatter = function (data) { return data };//y轴文字追加%

          option.legend.data = ['完成率', '未完成率', '按时完成率', '超期天数'];//设置图例文字
          option.xAxis.data = [this.queryInfo.factoryname];

          option.series[0] = _.merge(option.series[0], {
            name: '完成率',
            data: arr_completerate,
            barMaxWidth: 35,
            barGap: '70%',
            label: { color: colorList.blue.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option.series[1] = _.merge(_.cloneDeep(option.series[0]), {
            name: '未完成率',
            data: arr_notfinishrate,
            label: { color: colorList.red.color },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          option.series[2] = _.merge(_.cloneDeep(option.series[0]), {
            name: '按时完成率',
            data: arr_intimerate,
            label: { color: colorList.green.color },
            itemStyle: { color: { colorStops: colorList.green.colorStops } }
          });

          option.series[3] = _.merge(_.cloneDeep(option.series[0]), {
            name: '超期天数',
            data: arr_delaydayssum,
            yAxisIndex: 1,
            label: { color: colorList.yellow.color, formatter: '{c}天' },
            itemStyle: { color: { colorStops: colorList.yellow.colorStops } }
          });

          chart.setOption(option);
          // 解决边框组件宽高异常
          setTimeout(() => { this.$refs.factoryChart.initWH(); chart.resize(); }, 300);

          window.addEventListener('resize', () => {
            chart.resize();
          });

          chart.on('click', (params) => {
            const { seriesName, name, value } = params;
            const [start, end] = this.queryInfo.endtime;
            if (value == 0) { return; }
            this.chartDetail.queryInfo.currentPage = 1;
            this.chartDetail.queryInfo.seriesName = seriesName;
            this.chartDetail.queryInfo.name = name;
            this.chartDetail.queryInfo.orgType = '分厂';
            this.chartDetail.title = `【加工结束时间：${start} ~ ${end}】【${name}】【${seriesName}】明细`;
            this.chartDetail.dialogVisiable = true;
            this.getDetails();
          })
        },
        // 各工区排产统计
        async getWorkcenterData() {
          const dom = document.getElementById("workcenterChart");
          echarts.dispose(dom);
          let chart = echarts.init(dom);
          chart.showLoading(this.chartLoadingConfig);

          const { data: { data, meta, total } } = await axios.post('../handler/board/RbApsShipFinishDashBoard.ashx?action=getWorkcenterData', {
            type: 'getWorkcenterData',
            ...this.queryInfo
          })
          chart.hideLoading();
          if (meta.status == 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          echarts.dispose(dom);
          chart = echarts.init(dom);

          let arrWorkcenter = [], arr_completerate = [], arr_intimerate = [], arr_delaydayssum = [], arr_notfinishrate = [];
          for (const i of data) {
            arrWorkcenter.push(i.workcentername);
            arr_completerate.push(i.finishrate);//完成率
            arr_notfinishrate.push(i.notfinishrate);//未完成率
            arr_intimerate.push(i.intimefinishrate);//按时完成率
            arr_delaydayssum.push(i.delaydays);//超期天数
          }

          let option = barOption();
          option.grid = { left: '3%', right: '3%', bottom: '5%', containLabel: true };
          option.yAxis[0].axisLabel.formatter = function (data) { return data + "%"; };//y轴文字追加%
          option.yAxis[1].axisLabel.formatter = function (data) { return data };//y轴文字追加%

          option.legend.data = ['完成率', '未完成率', '按时完成率', '超期天数'];//设置图例文字
          option.xAxis.data = arrWorkcenter;

          option.series[0] = _.merge(option.series[0], {
            name: '完成率',
            data: arr_completerate,
            barMaxWidth: 35,
            barGap: '70%',
            label: { color: colorList.blue.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option.series[1] = _.merge(_.cloneDeep(option.series[0]), {
            name: '未完成率',
            data: arr_notfinishrate,
            label: { color: colorList.red.color },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          option.series[2] = _.merge(_.cloneDeep(option.series[0]), {
            name: '按时完成率',
            data: arr_intimerate,
            label: { color: colorList.green.color },
            itemStyle: { color: { colorStops: colorList.green.colorStops } }
          });

          option.series[3] = _.merge(_.cloneDeep(option.series[0]), {
            name: '超期天数',
            data: arr_delaydayssum,
            yAxisIndex: 1,
            label: { color: colorList.yellow.color, formatter: '{c}天' },
            itemStyle: { color: { colorStops: colorList.yellow.colorStops } }
          });

          chart.setOption(option);

          window.addEventListener('resize', () => {
            chart.resize();
          });

          // 解决边框组件宽高异常
          setTimeout(() => { this.$refs.workcenterChart.initWH(); chart.resize(); }, 300);
          chart.on('click', (params) => {
            const { seriesName, name, value } = params;
            const [start, end] = this.queryInfo.endtime;
            if (value == 0) { return; }
            this.chartDetail.queryInfo.currentPage = 1;
            this.chartDetail.queryInfo.seriesName = seriesName;
            this.chartDetail.queryInfo.name = name;
            this.chartDetail.queryInfo.orgType = '工区';
            this.chartDetail.title = `【加工结束时间：${start} ~ ${end}】【${name}】【${seriesName}】明细`;
            this.chartDetail.dialogVisiable = true;
            this.getDetails();
          })
        },
        // 各班组排产统计
        async getTeamData() {
          const dom = document.getElementById("teamChart");
          echarts.dispose(dom);
          let chart = echarts.init(dom);
          chart.showLoading(this.chartLoadingConfig);

          const { data: { data, meta, total } } = await axios.post('../handler/board/RbApsShipFinishDashBoard.ashx?action=getTeamData', {
            type: 'getTeamData',
            ...this.queryInfo
          })
          chart.hideLoading();
          if (meta.status == 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          echarts.dispose(dom);
          chart = echarts.init(dom);

          let arrTeam = [], arr_completerate = [], arr_intimerate = [], arr_delaydayssum = [], arr_notfinishrate = [];
          for (const i of data) {
            arrTeam.push(i.teamname);
            arr_completerate.push(i.finishrate);//完成率
            arr_notfinishrate.push(i.notfinishrate);//未完成率
            arr_intimerate.push(i.intimefinishrate);//按时完成率
            arr_delaydayssum.push(i.delaydays);//超期天数
          }

          let option = barOption();
          option.grid = { left: '3%', right: '3%', bottom: '5%', containLabel: true };
          option.yAxis[0].axisLabel.formatter = function (data) { return data + "%"; };//y轴文字追加%
          option.yAxis[1].axisLabel.formatter = function (data) { return data };//y轴文字追加%

          option.legend.data = ['完成率', '未完成率', '按时完成率', '超期天数'];//设置图例文字
          option.xAxis.data = arrTeam;

          option.series[0] = _.merge(option.series[0], {
            name: '完成率',
            data: arr_completerate,
            barMaxWidth: 35,
            barGap: '70%',
            label: { color: colorList.blue.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option.series[1] = _.merge(_.cloneDeep(option.series[0]), {
            name: '未完成率',
            data: arr_notfinishrate,
            label: { color: colorList.red.color },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          option.series[2] = _.merge(_.cloneDeep(option.series[0]), {
            name: '按时完成率',
            data: arr_intimerate,
            label: { color: colorList.green.color },
            itemStyle: { color: { colorStops: colorList.green.colorStops } }
          });

          option.series[3] = _.merge(_.cloneDeep(option.series[0]), {
            name: '超期天数',
            data: arr_delaydayssum,
            yAxisIndex: 1,
            label: { color: colorList.yellow.color, formatter: '{c}天' },
            itemStyle: { color: { colorStops: colorList.yellow.colorStops } }
          });

          chart.setOption(option);

          window.addEventListener('resize', () => {
            chart.resize();
          });

          // 解决边框组件宽高异常
          setTimeout(() => { this.$refs.teamChart.initWH(); chart.resize(); }, 300);
          chart.on('click', (params) => {
            const { seriesName, name, value } = params;
            const [start, end] = this.queryInfo.endtime;
            if (value == 0) { return; }
            this.chartDetail.queryInfo.currentPage = 1;
            this.chartDetail.queryInfo.seriesName = seriesName;
            this.chartDetail.queryInfo.name = name;
            this.chartDetail.queryInfo.orgType = '班组';
            this.chartDetail.title = `【加工结束时间：${start} ~ ${end}】【${name}】【${seriesName}】明细`;
            this.chartDetail.dialogVisiable = true;
            this.getDetails();
          })
        },
        // 明细
        async getDetails() {
          this.loading = true;
          this.loadingText = '数据加载中...';
          const { data: { data, meta, total } } = await axios.post('../handler/board/RbApsShipFinishDashBoard.ashx?action=getDetails', {
            type: 'getDetails',
            ...this.queryInfo,
            ...this.chartDetail.queryInfo,
            title: this.chartDetail.title
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.chartDetail.dataList = data;
          this.chartDetail.total = total;
        },
        handleChartDetailCurrentChange(newPage) {
          this.chartDetail.queryInfo.currentPage = newPage;
          this.getDetails();
        },
        // 分厂转工任务情况、各工区转工任务情况数据钻取
        exportDetail() {
          const config = {
            url: '../handler/board/RbApsShipFinishDashBoard.ashx?action=exportData',
            data: {
              type: 'exportData',
              ...this.queryInfo,
              ...this.chartDetail.queryInfo,
              title: this.chartDetail.title
            }
          }
          exportExcelData(config, this);
        },
      },
      async mounted() {
        this.chartHeight = (window.innerHeight - 120 - this.chartRow * 40) / this.chartRow;

        window.addEventListener('resize', () => {
          this.chartHeight = (window.innerHeight - 120 - this.chartRow * 40) / this.chartRow;
        });

        let incrementNumber = () => {
          setTimeout(incrementNumber, 1000);
          const weekDayText = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][dayjs().format('d')];
          this.currentTime = `${dayjs().format('YYYY年MM月DD日')} ${weekDayText} ${dayjs().format('HH:mm')}`;
        }

        setTimeout(incrementNumber, 500);
        const startOfThisMonth = dayjs().startOf('month').format('YYYY-MM-DD');
        const endOfThisMonth = dayjs().endOf('month').format('YYYY-MM-DD');
        this.queryInfo.endtime = [startOfThisMonth, endOfThisMonth];

        const factoryid = Decrypt(getParams('factoryid'));
        this.queryInfo.factoryid = factoryid;
        const factoryInfo = await getFactory({ factoryid: factoryid });
        if (factoryInfo) {
          const { factoryname } = factoryInfo[0];
          this.queryInfo.factoryname = factoryname;
          document.title = `${factoryname}APS排产统计看板`;
        }
        this.searchData();
      }
    })
  </script>
</body>

</html>